<template>
	<view>
    <view v-for="(item,i) in goodsList" :key='i' @click="gotoDetail(item)">
      <my-goods-item :goods='item'></my-goods-item>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数对象
				    queryObj: {
				      // 查询关键词
				      query: '',
				      // 商品分类Id
				      cid: '',
				      // 页码值
				      pagenum: 1,
				      // 每页显示多少条数据
				      pagesize: 50
				    },
            goodsList:[],
            total:0,
            // 防抖
            loading: false
			};
		},
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods:{
      async getGoodsList(cb) {
        this.loading = false
         const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
         if(res.meta.status!== 200) return uni.$message()
         this.goodsList = [...this.goodsList,...res.message.goods]
         this.total = res.message.total
         this.loading = true
         cb && cb()
      },
      // 下拉觸底
      onReachBottom() {
        // 頁碼加1
        if(!this.loading) return
        if(this.queryObj.pagenum * this.queryObj.pagesize > this.total) return uni.$message('沒有更多了')
        this.queryObj.pagenum ++
        this.getGoodsList()
      },
      onPullDownRefresh() {
        this.queryObj.pagenum = 1
        this.goodsList = []
        this.total = 0
        this.getGoodsList(()=>{
          uni.stopPullDownRefresh()
        })
      },
      // 点击跳转到商品详情页面
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    }
	}
</script>

<style lang="scss">

</style>
